/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package renders;

import entities.Alquileres;
import entities.Feriados;
import entities.Temporada;
import java.awt.Color;
import java.awt.Component;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import views.VistaDisponibilidad;

/**
 *
 * @author Smuggler
 */
public class DiaRender extends DefaultTableCellRenderer {
    Map<Integer, String> mapDias = new HashMap<Integer, String>();
    Map<Integer, String> mapMeses = new HashMap<Integer, String>();
    static int cont = 0;

    public Component getTableCellRendererComponent(JTable table,
            Object value,
            boolean isSelected,
            boolean hasFocus,
            int row,
            int column) {
        mapDias.put(1, "Domingo");
        mapDias.put(2, "Lunes");
        mapDias.put(3, "Martes");
        mapDias.put(4, "Miercoles");
        mapDias.put(5, "Jueves");
        mapDias.put(6, "Viernes");
        mapDias.put(7, "Sabado");
        mapMeses.put(0, "Ene");
        mapMeses.put(1, "Feb");
        mapMeses.put(2, "Mar");
        mapMeses.put(3, "Abr");
        mapMeses.put(4, "May");
        mapMeses.put(5, "Jun");
        mapMeses.put(6, "Jul");
        mapMeses.put(7, "Ago");
        mapMeses.put(8, "Sep");
        mapMeses.put(9, "Oct");
        mapMeses.put(10, "Nov");
        mapMeses.put(11, "Dic");
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        this.setBackground(isSelected ? Color.CYAN : Color.WHITE);
        Date fecha = (Date) table.getModel().getValueAt(row, 0);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(fecha);
        String nombreDia = mapDias.get(calendar.get(Calendar.DAY_OF_WEEK));
        String nombreMes;
        int numeroAnio;
        nombreMes = mapMeses.get(calendar.get(Calendar.MONTH));
        numeroAnio = calendar.get(Calendar.YEAR);
        String tempMedia = "MEDIA";
        String tempAlta = "ALTA";
        Calendar cal;
        cal = Calendar.getInstance();
        int mes;
        int dia;

        if (value != null) {
            if ((calendar.get(Calendar.DAY_OF_WEEK) == 1) && !(isSelected) && (column != 0)) {
                this.setBackground(Color.LIGHT_GRAY);
            }
            //Si la columna es de uno de los departamentos
            if (column >= 2) {
                Calendar inicioReserva, finReserva, cal3;
                inicioReserva = Calendar.getInstance();
                finReserva = Calendar.getInstance();
                cal3 = Calendar.getInstance();

                this.setValue("");
                for (Alquileres alquiler : VistaDisponibilidad.alquileres) {
                    inicioReserva.setTime(alquiler.getFechaArribo());
                    finReserva.setTime(alquiler.getFechaPartida());
                    cal3.setTime(fecha);
                    if ((cal3.compareTo(inicioReserva) >= 0) && (cal3.compareTo(finReserva) <= 0) && (alquiler.getIdUnidadAlojamiento().getNombre().equals(table.getColumnName(column)))) {
                        switch (alquiler.getTipoAlquiler()) {
                            case "F":
                                this.setBackground(new Color(154, 205, 50));
                                this.setValue(/*alquiler.getTipoAlquiler() + " " + */alquiler.getIdCliente().getApellido() + " " + alquiler.getIdCliente().getNombres());
                                break;
                            case "R":
                                this.setBackground(new Color(238, 238, 0));
                                this.setValue(/*alquiler.getTipoAlquiler() + " " + */alquiler.getIdCliente().getApellido() + " " + alquiler.getIdCliente().getNombres());
                                break;
                            case "M":
                                this.setBackground(new Color(255, 128, 0));
                                this.setValue("En Mantenimiento");
                                break;
                            case "O":
                                //ROJO 
                                this.setBackground(new Color(220, 20, 60));
                                this.setValue(/*alquiler.getTipoAlquiler() + " " + */alquiler.getIdCliente().getApellido() + " " + alquiler.getIdCliente().getNombres());
                                break;
                        }
                    }
                }
            }
            switch (column) {
                case 0:
                    cal.setTime((Date) value);
                    mes = cal.get(Calendar.MONTH) + 1;
                    dia = cal.get(Calendar.DATE);
                    boolean encontrado = false;

                    this.setBackground(Color.YELLOW);

                    Calendar cal1 = Calendar.getInstance();
                    cal.setTime((Date) value);
                    cal.set(Calendar.HOUR_OF_DAY, 0);
                    cal.set(Calendar.MINUTE, 0);
                    cal.set(Calendar.SECOND, 0);
                    cal.set(Calendar.MILLISECOND, 0);
                    ((Date) value).setTime(cal.getTimeInMillis());
                    for (Temporada temp : VistaDisponibilidad.temporada) {

                        if (((((Date) value).compareTo(temp.getInicio()) >= 0) && (((Date) value).compareTo(temp.getFin()) <= 0)) && !encontrado) {
//                         
                            this.setBackground(new Color(temp.getTipoTemporadas().getRGBColor()));
                            encontrado = true;
                        }
                    }
                    this.setValue(nombreMes + " " + numeroAnio);
                    break;
                case 1:
                    Calendar cal2 = Calendar.getInstance(),
                     cal3 = Calendar.getInstance();
                    for (Feriados feriado : VistaDisponibilidad.feriados) {
                        cal2.setTime(feriado.getFecha());
                        cal3.setTime(fecha);
                        if ((cal2.get(Calendar.YEAR) == cal3.get(Calendar.YEAR)) && (cal2.get(Calendar.MONTH) == cal3.get(Calendar.MONTH)) && (cal2.get(Calendar.DATE) == cal3.get(Calendar.DATE))) {
                            this.setBackground(Color.GREEN);
                        }
                    }
                    this.setValue(nombreDia + " " + calendar.get(Calendar.DAY_OF_MONTH));
                    break;
                default:

            }
            if (calendar.get(Calendar.DAY_OF_MONTH) == 1) {
                setBorder(BorderFactory.createMatteBorder(4, 0, 0, 0, Color.black));
                setText(" " + this.getText());
            }

        }

        return this;
    }
}